Register-Lib PowerShell.Commands

# .SYNOPSIS
# Gets any aliases to the given command
function Get-AliasesForCommand (
    [string]
    # The name of a PowerShell command 
    $Name
){ 
    alias | where { $_.Definition -match $Name } 
}

Set-Alias gra Get-AliasesForCommand -Option AllScope


<#
.SYNOPSIS
Get the body of the named command

.NOTES
This only works for commands that were defined in .PS1 scripts

#>
Set-Alias gd Get-Definition -Option AllScope
function Get-Definition {
	param($name) 
	$cmd = Get-Command $name
	if ($cmd.CommandType -eq "Alias") {
		Log "$name is an Alias for $($cmd.Definition)"
		return Get-Definition $cmd.Definition
	} else {
	return $cmd | fl *
	}
}


<#
.SYNOPSIS
List commands that were (probably) defined by local or session scripts rather than Cmdlets that
are defined by PS Snapins.
#>
function Get-UDFs {
    get-command | ? { !($_.ModuleName) -and !($_.HelpUri)}
}

<#
.SYNOPSIS
Writes out the definition of the PS function that is piped into it
#>
filter Write-FunctionDef() { 
    $fn = $_ 
    if( @('function','filter') -contains $fn.CommandType) {
        'function ' + $fn.Name + '{'
        $fn.Definition.Split("`r`n", [StringSplitOptions]::RemoveEmptyEntries) | % { 
            #Tabbing isn't safe because of herestrings
            #$_ = "`t" + $_
            
             $_
        }
        "}`n"
    } elseif ($fn.CommandType -eq 'Alias') {
        "set-alias -name '$($fn.Name)' -value '$($fn.Definition)`n'"
    }
      
}

Set-Alias gpsc Get-PSCommand -Option AllScope 
Set-Alias fpsc Find-PSCommand -Option AllScope

Set-Alias Find-Command Find-PSCommand -Option AllScope
function Find-PSCommand($pattern) {
	Get-PSCommand | where { $_.Name -match $pattern -or $_.Definition -match $pattern }
}

Set-Alias fa Find-Application -Option AllScope
function Find-Application($pattern) {
	Get-Command -c application  | ? {$_.Name -match $pattern}
}
